import { atom, selector } from 'recoil'
import { isNullData } from '@/utils'
import { dict } from '@/pages/user/login/service/service'

type Option = {
  label: string
  value: string
}

type Options = Option[]

type Data = {
  is_teach_hospital: Options
  platform_set_type: Options
  hospital_nature: Options
  hospital_rating: Options
  hospital_type: Options
  platform_account_type: Options
  hospital_level: Options
  quarter: Options
  operator: Options
  format: Options
  graph: Options
  sms_template_head: Options
  email_template_head: Options
  email_msg_type: Options
  sex: Options
  // 提交状态
  report_status: Options
  assessment_tool: Options
  assessment_level: Options
  distance_time: Options
  age: Options
  // 审核状态
  apply_status: Options
  platform_join_hospital_audit_status: Options
}

// 存储字典所有值
export const dictValues = atom({
  key: 'dictValues',
  default: <Data>{},
})

// import { useRecoilValue } from 'recoil'
// import { dictValuesSelector } from '@/store/dicts'
// const dictValuesSelectorValues = useRecoilValue(dictValuesSelector)

// md 字典缓存一下
export const dictValuesSelector = selector({
  key: 'dictValuesSelector',
  get: async (): Promise<any> => {
    const res = await dict()
    const filat: Data = res.data?.reduce((acc: any, item: any) => {
      const key = Object.keys(item)[0]
      const value = item[key]
      acc[key] = value
      return acc
    }, {})
    // console.log('看一下三级联动数据是什么样子', res.data)
    if (isNullData(res)) {
      return filat
    }
    return []
  },
})
